<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * --------------------------------------------------------------------------
 * ADMIN BLOG KATEGORI CONTROLLER
 * --------------------------------------------------------------------------
 *
 * @author Ali
 * */
class Blog_category extends Admin_Controller {

	public function __construct()
	{
        parent::__construct();
        
        // load related files
        $this->load->model('backend/blog_category_model');
    }

	public function index()
	{    
        $result = $this->blog_category_model->gets();
        
        $data = array(
          'result'   => $result
        );
        
        $this->template->load('blog_category/index', $data);
    }

	public function add()
	{
        if(!$this->input->is_ajax_request()){
            
            $categories = array('Ana Kategori') + $this->blog_category_model->tree_selectbox();
            
            $data = array(
                'categories' => $categories
            );
            
            $this->template->load('blog_category/add', $data);
            
        } else {
			
            // VALIDTION
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('parent_id', 'Üst kategori', 'trim|required|numeric');
            $this->form_validation->set_rules("image", 'Kategori Resmi', 'trim|required|callback__check_file');
            $this->form_validation->set_rules('title', 'Kategori Adı', 'trim|required|xss_clean|max_length[255]');
            $this->form_validation->set_rules('description', 'Açıklama', 'trim|required');
            
			if(! $this->form_validation->run())
			{
            	ajax_response($this->form_validation->error_string());
            }
            
            // NO ERRORS, CONTINUE
            
            
            $data = array(
                'parent_id' => $this->input->post('parent_id'),
                'image' => global_filepath($this->input->post('image')),
                'title' => $this->input->post('title'),
                'slug' => url_title($this->input->post('title')),
                'description' => $this->input->post('image'),
                'order' => $this->blog_category_model->get_order_number()
            );
            
            $this->blog_category_model->add($data);
			
			$msg = "Kategori başarıyla eklendi.";
			set_notify($msg);
			
			ajax_response($msg, FALSE, site_url('backend/blog_category/index'));
        }
        
    }
    
	public function edit($id)
	{
		if(empty ($_POST))
		{
            $cateogory = $this->blog_category_model->get($id) OR show_error('Kategori bulunamadı');
            
            $categories = array('Ana Kategori') + $this->blog_category_model->tree_selectbox(0, 0, $cateogory->id);
            
            $data = array(
                'categories' => $categories,
                'category' => $cateogory
            );
            
            $this->template->load('blog_category/edit', $data);
            
        } else {
            
            // VALIDTION
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('category_id', 'Kategori ID', 'trim|required|numeric');
            $this->form_validation->set_rules('parent_id', 'Üst kategori', 'trim|required|numeric');
            $this->form_validation->set_rules("image", 'Kategori Resmi', 'trim|required|callback__check_file');
            $this->form_validation->set_rules('title', 'Kategori Adı', 'trim|required|xss_clean|max_length[255]');
            $this->form_validation->set_rules('description', 'Açıklama', 'trim|required');
            
			if(! $this->form_validation->run())
			{
                ajax_response($this->form_validation->error_string());
            }
            
            // NO ERRORS, CONTINUE
            
            
            $data = array(
                'parent_id' => $this->input->post('parent_id'),
                'image' => global_filepath($this->input->post('image')),
                'title' => $this->input->post('title'),
                'slug' => url_title($this->input->post('title')),
                'description' => $this->input->post('image')
            );
            
            $this->blog_category_model->edit($data, $this->input->post('category_id'));
            
			$msg = 'Kategori başarıyla düzenlendi.';
			set_notify($msg);

			ajax_response($msg, FALSE, site_url('backend/blog_category/index'));
        }
        
        
    }
    
	public  function delete($id)
	{    
        $this->blog_category_model->delete($id);

		set_notify('Kategori başarıyla silindi');	
        
        redirect($this->input->server('HTTP_REFERER'));
    }
    
    /**
     * ----------------------------------------------------------------------
     * CALLBACKS
     * ----------------------------------------------------------------------
     */
    
    /**
     * Formda girilen yolun gerçekten bir dosyaya ait olup olmadığını sınar.
     * 
     * @param string $file
     * @return bool 
     */
	public function _check_file($file)
	{
        $file = global_filepath($file);

        if (is_file($file))
            return true;

        $this->form_validation->set_message('_check_file', 'Kapak resmi geçersiz.');
        return FALSE;
    }    

}

/* End of file Blog_category.php */
